﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Chinesys.UI.WebService;

namespace Chinesys.UI.Exchange
{
    public partial class CurrencyExchange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.PageTitle = "Servicio de Cotización OnLine";

            if (!IsPostBack)
            {
                // Load Currencies...
                for (int i = 0; i < CurrencyList.Count; i++)
                {
                    DropDownListFrom.Items.Add(CurrencyList.Descriptions[i]);
                    DropDownListFrom.Items[i].Value = CurrencyList.Codes[i];

                    DropDownListTo.Items.Add(CurrencyList.Descriptions[i]);
                    DropDownListTo.Items[i].Value = CurrencyList.Codes[i];
                }

                // Set default currencies or load them from cookies:
                if (Request.Cookies["CurrencyConverter"] == null)
                {
                    DropDownListFrom.SelectedValue = "USD";
                    DropDownListTo.SelectedValue = "RUB";
                }
                else
                {
                    DropDownListFrom.SelectedValue = Request.Cookies["CurrencyConverter"]["from"];
                    DropDownListTo.SelectedValue = Request.Cookies["CurrencyConverter"]["to"];
                }
            }
        }

        protected void ConvertCurrencies(object sender, EventArgs e)
        {
            TableData.Visible = false;

            TableMessage.Visible = false;

            CurrencyConverter cc = new CurrencyConverter();

            cc.AdjustToLocalTime = CheckBoxAdjust.Checked;

            // Add a proxy here is needed. Example:
            // cc.Proxy = new System.Net.WebProxy("0.0.0.0", 80);

            try
            {
                CurrencyData cd = cc.GetCurrencyData(DropDownListFrom.SelectedValue, DropDownListTo.SelectedValue);

                LabelResFrom.Text = cd.BaseCode;
                LabelResTo.Text = cd.TargetCode;
                LabelResPrice.Text = cd.Rate.ToString();
                LabelResDate.Text = cd.TradeDate.ToString();
                LabelResMin.Text = cd.Min.ToString();
                LabelResMax.Text = cd.Max.ToString();

                LabelMsg.Text = "Converción Exitosa!";
                LabelMsg.ForeColor = System.Drawing.Color.DarkGreen;
                TableMessage.BackColor = System.Drawing.Color.LightGreen;
                TableMessage.Visible = true;

                TableData.Visible = true;

                // Save settings to cookies:
                Response.Cookies["CurrencyConverter"]["from"] = DropDownListFrom.SelectedValue;
                Response.Cookies["CurrencyConverter"]["to"] = DropDownListTo.SelectedValue;
                Response.Cookies["CurrencyConverter"].Expires = DateTime.Now.AddDays(7);
            }
            catch (Exception ex)
            {
                LabelMsg.Text = ex.Message;
                LabelMsg.ForeColor = System.Drawing.Color.DarkRed;
                TableMessage.BackColor = System.Drawing.Color.LightPink;
                TableMessage.Visible = true;
            }

        }
    }
}